package client.gui;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Rectangle;
import java.io.IOException;

import javax.swing.JTextField;
import javax.swing.JLabel;

import client.ChatClient;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Color;
import java.awt.Toolkit;


/**
 * This is the Main Screen that responsible to get the server connection info
 * @author Amir S.
 * @author Jacob C.
 * @author Maya G.
 * @author Michael V.
 *
 */
public class Client_GUI extends JFrame {

	
	private static final long serialVersionUID = 1L;
	
	//GUI attributes
	public GUI_Login lg = null;
	public GUI_Guest gst = null;
	public GUI_Student stdnt = null;
	public GUI_Teacher tchr = null;
	public GUI_Manager mngr = null;
	public GUI_Admin admn = null;
	public GUI_Teacher_Add_Question taq = null;
	public GUI_Manager_ViewQuestions mvq = null;
	public GUI_Manager_ViewExams mve = null;
	public GUI_Manager_ViewGrades mvg = null;
	public GUI_CreateTest ct = null;
	public GUI_SelectSubjectTest cst=null;
	public GUI_ComputerizeTest cmpt = null;
	public GUI_Student_ViewGrades svg = null;
	public GUI_TeacherApprovingGrade tag = null;
	public GUI_Teacher_Delete_Question tdq = null;
	public GUI_Teacher_Edit_Question teq = null;
	public GUI_Teacher_Edit_Question2 teq2 = null;
	public GUI_Teacher_Execute_Exam texm=null;
	public GUI_Teacher_Extension_Request ter = null;
	public GUI_Manager_Statistics mst = null;
	public GUI_ManualTest mt = null;
	public GUI_Teacher_View_Statistics tvs = null;
	public GUI_Teacher_ViewGrades tvg = null;
	protected GUI_Create_Manual_Test cmt=null;
	//End of gui attributes
	
	private Toolkit toolkit = Toolkit.getDefaultToolkit();
	private Dimension scrnsize = toolkit.getScreenSize();
	
	private GUI_ImagePanel background;
	
	private JTextField PortTextField;
	private JTextField IPtextfield;
	
	private JPanel FirstPanel = null;
	private JButton Exit = null;
	private JButton Connectbt = null;
	
	private JLabel Port = null;
	private JLabel IP = null;
	private JLabel jLabel = null;
	//Client-Server attributes
	public String serverIP="localhost";
	public int serverPort=5555;
	
	public ChatClient client;

	

	
	
	/**
	 * This method initializes Connectbt button
	 * which connects the client to server. 
	 * @throws Exception
	 */
	private JButton getConnectbt() {
		if (Connectbt == null) {
			Connectbt = new JButton();
			Connectbt.setText("Connect");
			Connectbt.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 20));
			Connectbt.setBounds(new Rectangle(694, 237, 146, 34));
			Connectbt.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					
					try {	
						
						connect();
						
					} catch (IOException e1) {
						
						JOptionPane.showMessageDialog(FirstPanel, "Unable to connect! \n Please check connection", "ERROR", JOptionPane.ERROR_MESSAGE);
						e1.printStackTrace();
					} catch (Exception e2) {
						
						JOptionPane.showMessageDialog(FirstPanel, "Unable to connect! \n Please insert legal server IP and PORT format", "ERROR", JOptionPane.ERROR_MESSAGE);

						e2.printStackTrace();
					}
					
					
					setContentPane(lg);
							
				}
			});
		}
		return Connectbt;
	}
	/**
	 * creates a  server IP through a specific course
	 * @throws Exception
	 */
	public void connect() throws Exception {
		
		if(IPtextfield.getText().equals("") || PortTextField.equals(""))
		{	
			Exception e = new Exception();
			throw(e);
		}
			
			serverIP=IPtextfield.getText();
			serverPort=Integer.parseInt(PortTextField.getText());
			client = new ChatClient(serverIP,serverPort);
			System.out.println("Client connected to " + serverIP + " through port " + serverPort);
			activateLogin();
	}

	
	/**
	 * This is the default constructor
	 */
	public Client_GUI() {
		super();
		initialize();
		
	}

	/**
	 * This method initializes the Main screen . 
	 */
	private void initialize() {
		
	
		this.setSize(1000, 700);
		this.setLocation((toolkit.getScreenSize().width-1000)/2, (toolkit.getScreenSize().height-700)/2);
		this.setContentPane(getFirstPanel());
		this.setTitle("AES System");
		
	}

	/**
	 * This method sends the client details.
	 */
	public void activateLogin()
	{
		lg=new GUI_Login(this);
	}
	
	
	/**
	 * This method initializes FirstPanel
	 * 
	 */
	public JPanel getFirstPanel() {
		if (FirstPanel == null) {
			jLabel = new JLabel();
			jLabel.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 40));
			jLabel.setBounds(new Rectangle(222, 29, 566, 57));
			jLabel.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 35));
			jLabel.setForeground(Color.BLUE);
			jLabel.setText("Welcome to AES System");
			
			IP = new JLabel();
			IP.setSize(new Dimension(141, 31));
			IP.setLocation(new Point(309, 211));
			IP.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 25));
			
			IP.setForeground(Color.BLUE);
			IP.setText("IP Address:");
			
			Port = new JLabel();
			Port.setSize(new Dimension(136, 37));
			Port.setLocation(new Point(309, 258));
			Port.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 25));
			Port.setForeground(Color.BLUE);
			Port.setText("Port:");
			
			FirstPanel = new JPanel();
			FirstPanel.setLayout(null);
			FirstPanel.add(getConnectbt(), null);
			FirstPanel.add(getPortTextField(), null);
			FirstPanel.add(getIPtextfield(), null);
			FirstPanel.add(Port, null);
			FirstPanel.add(IP, null);
			FirstPanel.add(jLabel, null);
			FirstPanel.add(getExit());
		
			
			this.background = new GUI_ImagePanel(new ImageIcon("images/background1.jpg").getImage());
			FirstPanel.add(background,null);
		}
		return FirstPanel; 
	}
	


	/**
	 * This method initializes PortTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getPortTextField() {
		if (PortTextField == null) {
			PortTextField = new JTextField();
			PortTextField.setVisible(true);
			PortTextField.setText("5555");
			PortTextField.setFont(new Font("Arial", Font.BOLD, 25));
			PortTextField.setSize(new Dimension(200, 38));
			PortTextField.setLocation(new Point(465, 257));
		}
		return PortTextField;
	}


	/**
	 * This method initializes IPtextfield	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getIPtextfield() {
		if (IPtextfield == null) {
			IPtextfield = new JTextField();
			IPtextfield.setVisible(true);
			IPtextfield.setText("localhost");
			IPtextfield.setFont(new Font("Arial", Font.BOLD, 25));
			IPtextfield.setSize(new Dimension(200, 35));
			IPtextfield.setLocation(new Point(465, 211));
		}
		return IPtextfield;
	}
	
	
	/**
	 * This method initializes Get client data
	 * which shows the currently connected clients to 
	 * the server. 
	 * 	
	 * @return javax.swing.JTextField	
	 */
	public void getclientData()
	{
		this.serverIP=IPtextfield.getText();
		this.serverPort=Integer.parseInt(PortTextField.getText());
	}
	
	private JButton getExit() {
		if (Exit == null) {
			Exit = new JButton();
			Exit.setText("Exit");
			Exit.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 20));
			Exit.setBounds(new Rectangle(396, 346, 146, 34));
			Exit.setActionCommand("Exit");
			Exit.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					int i = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit AES System?", "AES System", JOptionPane.YES_NO_OPTION);
					if (i==JOptionPane.YES_OPTION)
					{
						System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
						JOptionPane.showMessageDialog(null,"Thanks For Using AES System");
						System.exit(ABORT);
					}
				}
			});
		}
		return Exit;
	}
	
	

}  
